<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 景点接口
 */
class Controller_Pc_Api_V2_Ferry extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Ferry
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Ferry
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('ferry', $this->request_params);

        // 服务
        $this->service = new Service_Api_Ferry();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 查询
    public function action_query()
    {
        try {
            $this->validation->query()->check();

            if (!empty($this->request_params['tag']) && $this->request_params['tag'] != 'near') {
                unset($this->request_params['lat'], $this->request_params['lng'], $this->request_params['distance']);
            }

            $this->request_params['platform_no'] = $this->platform_no;
            $result               = $this->service->search($this->request_params);
            $this->result['data'] = $result;

        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 查询条件
    public function action_query_condition()
    {
        try {
            $result = $this->service->query_condition();

            $this->result['data'] = $result;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }


    // 详情
    public function action_get_detail()
    {
        try {
            $this->validation->get_detail()->check();

            $params['id']     = $this->request_params['id'];
            $params['userid'] = $this->mid;
            $params['platform_no']  = $this->platform_no;
            $params['card_suit_id'] = $this->request_params['card_suit_id'];

            $data = $this->service->info($params);

            if (empty($data)) {
                throw new Exception('未找到景点信息');
            }

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 套餐详情
    public function action_suit_info()
    {
        try {
            $this->validation->suit_info()->check();

            $this->result['data'] = $this->service->suit_info($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 预订
    public function action_add_order()
    {
        try {
            $this->validation->add_order()->check();

            $this->request_params['member_info'] = $this->member_info;
            $order_info                          = $this->service->create_order($this->request_params);

            $this->result['msg']  = '订单创建成功';
            $this->result['data'] = $order_info;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
            $this->result['data']   = $e->getTraceAsString();
        }
    }

    // 单景点首页模板
    public function action_get_hot_product()
    {
        try {
            $this->request_params['platform_no']=$this->platform_no;
            $product_list = $this->service->hot_products($this->request_params);

            $this->result['data']['product_list'] = $product_list;
        } catch (Exception $e) {
            $this->result['msg']    = $e->getMessage();
            $this->result['status'] = false;
        }
    }

    // 预订日历
    public function action_book_calendar()
    {
        try {
            $this->validation->book_calendar()->check();

            $this->result['data'] = $this->service->book_calendar($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 预订套餐
    public function action_book_suit() {
        try {
            $this->validation->book_suit()->check();

            $this->result['data'] = $this->service->book_suit($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 预订套餐规格
    public function action_book_sku() {
        try {
            $this->validation->book_sku()->check();

            $this->result['data'] = $this->service->book_sku($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }
}